<?php

namespace App\Http\Controllers\ShopAdmin;

use App\Models\AdminAction;
use App\Models\AdminRole;
use App\Models\AdminUser;
use App\Models\ShopUser;
use Illuminate\Http\Request;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests;
use App\Http\Requests\ShopAdmin\Login\LoginRequest;
use App\Http\Controllers\Controller;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\MessageBag;
use Auth,Session,Log;
use App\Models\User;
use App\Commands\OperationLogCommand;

class LoginController extends Controller
{

    public function index()
    {
        return view('shop_admin.login.index');
    }


    public function store(LoginRequest $loginRequest,MessageBag $error)
    {

        if(empty($loginRequest->get('chkCode')) || trim($loginRequest->get('chkCode')) != trim(Session::get('shopadmincaptcha'))){

            $error->add('result','验证码不正确');

            return back()->withErrors($error);
        }
        // $shopAdminUser = User::where('user_name',$loginRequest->get('user_name'))->where('role_id', '>', 0)->first();

        $shopAdminUser = User::where('user_name',$loginRequest->get('user_name'))->first();

        if(empty($shopAdminUser)){

            $error->add('result','用户不存在');

            return back()->withErrors($error);
        }else{


            $shopUser = ShopUser::where('user_id', $shopAdminUser->id)->first();

            if(empty($shopUser)){

                $error->add('result','用户无商铺后台权限');

                return back()->withErrors($error);
            }else{

                if (md5($loginRequest->get('password'))===$shopAdminUser->password&&$shopUser){

                    Auth::login($shopAdminUser);
                    Session::put('shopadmincaptcha', "");
                    Session::put('userShopId', $shopUser->shop_id);
                    $this->dispatch(
                        new OperationLogCommand('登录了商铺后台','User','id:'.$shopAdminUser->id)
                    );

                    // return redirect()->route('shop_admin.home');
                    return redirect()->route('shop_admin.goods.index');

                }else{
                    $error->add('result','用户名或密码错误');

                    return back()->withErrors($error);
                }
            }
        }
    }

    public function destroy($id){

        $this->dispatch(
            new OperationLogCommand('退出了商铺后台','User','id:'.$id)
        );
        Auth::logout();

        if (Session::has('userShopId')){

            Session::forget('userShopId');
        }
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function captcha($tmp,CaptchaBuilder $builder)
    {
        //图片效果设置样放在 build 方法前面
        $builder->setMaxBehindLines(2);                 //背景干扰线
        $builder->setMaxFrontLines(0);                  //前置干扰线
        $builder->setMaxOffset(6);                      //设置偏移值
        $builder->setDistortion(false);                //是否失真

        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 38, $font = null);

        //获取验证码的内容
        $phrase = $builder->getPhrase();
        //把内容存入session
        Session::put('shopadmincaptcha', $phrase);
        //Session::flash('shopadmincaptcha', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
        $builder->inline();
    }


}
